//Bhaskar added this function on 6 December
function checkLotNumber()
{
	var p1 = -1;
	var p2 = -1;
	var lotName = '';
	var flag = true;
	for (var count =0; count < document.assemblyDataEntry.lotId.length; count++ )
	{
		p1 = document.assemblyDataEntry.lotId[count].value.indexOf('$')+1;
		p2 = document.assemblyDataEntry.lotId[count].value.lastIndexOf('$');
		lotName = document.assemblyDataEntry.lotId[count].value.substring(p1, p2).trim();
		if (lotName.toUpperCase() == document.assemblyDataEntry.lotNo.value.trim().toUpperCase())
		{
			flag = false;
			document.assemblyDataEntry.lotNo.value = document.assemblyDataEntry.lotNo.value.trim();
			break;
		}
	}
	return flag;
}



function isFloat(s)
{
	var i;
    var seenDecimalPoint = false;
	var decimalPointDelimiter = ".";
	if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


function locationSelected(form)
{
    var pa=document.getElementById("message");
    var x = form.locationId;

    for(var i=0; i< x.length; i++)
    {
        if(x.options[i].selected)
        {
	        if(x.options[i].value == "0")
            {
	            pa.innerHTML = "<li>Select Location";
                x.focus();
	            return false;
            }
        }
    }
    return true;
}

function productSelected(form)
{
    var pa=document.getElementById("message");
    var x = form.processStepId;

    for(var i=0; i< x.length; i++)
    {
        if(x.options[i].selected)
        {
	        if(x.options[i].value == "0")
            {
	            pa.innerHTML = "<li>Select Product Name";
                x.focus();
	            return false;
            }
        }
    }
    return true;
}

function lotNoSelected(form){

    var pa=document.getElementById("message");
    var x = form.lotId;

    for(var i=0; i< x.length; i++)
    {
        if(x.options[i].selected)
        {
	        if(x.options[i].value == "0")
            {
	            pa.innerHTML = "<li>Select Lot Number";
                x.focus();
	            return false;
            }
        }
    }
    return true;
}


//Bhaskar added this method on 06 Feb 2004
function calculateYield( abbrev )
{
	/*
	Bhaskar on 06 Feb 2004
	A very important feature right now incorporated is like this. Based on the third argument that is passed,
	the appropriate yield is calculated and placed in that corresponding field in the front end.
	DD = Dice Discrepancy
	DI = Dice Inspect
	PM = Pre Mold
	IF = IC Finished
	the word abbrev that is passed in this method as an argument is meant to be understood as "abbreviation"
	*/
	var pa=document.getElementById("message");
	var quantityIn = '0';//direct values are being passed in.
	var quantityOut = '0';//direct values are being passed in.
	var quantityOutObj;
	if ( abbrev == 'DI' )
	{
		quantityIn = eval ('document.assemblyDataEntry.diceInspectIn').value;
		quantityOut = eval ('document.assemblyDataEntry.diceInspectOut').value;
	}
	if ( abbrev == 'IF' )
	{
		quantityIn = eval ('document.assemblyDataEntry.icFinishedIn').value;
		quantityOut = eval ('document.assemblyDataEntry.icFinishedOut').value;
	}
	if ( abbrev == 'IF' )
	{
		quantityIn = eval ('document.assemblyDataEntry.icFinishedIn').value;
		quantityOut = eval ('document.assemblyDataEntry.icFinishedOut').value;
	}
	if ( abbrev == 'YC' )
	{
		quantityIn = eval ('document.assemblyDataEntry.quantityIn').value;
		quantityOut = eval ('document.assemblyDataEntry.quantityOut').value;
	}


	if( quantityIn.length == 0 && quantityOut.length == 0 )
	{
		return ;
	}

	if( quantityIn.length > 0 && quantityOut.length == 0 )
	{
		return ;
	}

	/*if(quantityIn!='' && quantityOut!='')
	{
		/*if (!isInteger(quantityIn)){
			pa.innerHTML = "Please enter valid Quantity In,Quantity In must be number";
			return;
		}

		if (!isInteger(quantityOut)){
			pa.innerHTML = "Please enter valid Quantity Out,Quantity Out must be number";
			return;
		}

		if ( parseInt ( quantityOut ) == 0 )
		{
			return;
		}

		if (parseInt(quantityOut) > parseInt(quantityIn)){
			pa.innerHTML = "Please enter valid Quantities, Quantity Out cannot be greater, Quantity In.";
			return;
		} */

		var CalYield = parseInt(quantityOut)/parseInt(quantityIn)*100;

		if ( abbrev == 'DI' )
		{
		    if ( parseFloat(CalYield) && CalYield != 'Infinity' )
		    {
			    document.assemblyDataEntry.diceInspectYield.value = formatYieldIntoDoubleDecimalMode ( CalYield );
			    //document.assemblyDataEntry.preMouldIn.value = quantityOut;
			}
			else
			{
			    document.assemblyDataEntry.diceInspectYield.value = "";
			}
		}
		else if ( abbrev == 'IF' )
		{
		    if ( parseFloat(CalYield) && CalYield != 'Infinity' )
		    {
			    document.assemblyDataEntry.icFinishedYield.value = formatYieldIntoDoubleDecimalMode ( CalYield );
			}
			else
			{
			    document.assemblyDataEntry.icFinishedYield.value = "";
			}
		}
		else if ( abbrev == 'YC' )
		{
		    if ( parseFloat(CalYield) && CalYield != 'Infinity' )
		    {
			    document.assemblyDataEntry.asyYield.value = formatYieldIntoDoubleDecimalMode ( CalYield );
			}
			else
			{
			    document.assemblyDataEntry.asyYield.value = "";
			}
		}
	//}

	pa.innerHTML = "";
}


/*
Bhaskar added this function on 05 Feb 2004
*/
function formatYieldIntoDoubleDecimalMode ( yieldValu )
{
	var yldValue = yieldValu;
	yldValue = ''+(Math.round(yieldValu*100))/100;
	var p1 = yldValue.indexOf('.');
	if ( parseInt ( p1 ) == -1 )
	{
		yldValue += '.00'
	}
	else if ( parseInt ( p1 ) != -1 )
	{
		var p2 = yldValue.substring ( p1, yldValue.length );
		if ( parseInt( p2.length ) < 3 )
		{
			yldValue += '0';
		}
	}
	return yldValue;
}


function recheckYields ( qtyInObj, qtyOutObj, yieldObj )
{
	var qtyIn = 0;
	var qtyIn = 0;
	var pa=document.getElementById("message");
	pa.innerHTML = '';

	qtyIn = qtyInObj.value.trim();
	qtyOut = qtyOutObj.value.trim();

	if(qtyIn!='' && qtyOut!='')
	{
		if ( ( parseInt (qtyIn) == 0) && ( parseInt (qtyOut) == 0 ) )
		{
			//pa.innerHTML = "Both Quantity In, Quantity Out cannot be Zero";
			qtyInObj.value = '0';
			qtyOutObj.value = '0';
			yieldObj.value = '0.00';
			return true;
		}

		if ( parseInt (qtyIn) < parseInt (qtyOut) )
		{
			pa.innerHTML = "Please enter valid Quantities. Quantity Out cannot be greater.";
			qtyOutObj.focus();
			return false;
		}
		else
		{
			if ( parseInt (qtyOut) > 0 )
			{
				var calcYield = parseInt(qtyOut)/parseInt(qtyIn)*100;
				yieldObj.value = formatYieldIntoDoubleDecimalMode(calcYield);
			}
			else if ( parseInt (qtyOut) == 0 )
			{
				yieldObj.value = '0.00';
			}
		}
	}
	return true;
}


function checkAllYields()
{
	//var dice_inspect_in		=document.assemblyDataEntry.diceInspectIn.value;
	//var dice_inspect_out	=document.assemblyDataEntry.diceInspectOut.value;
	//var dice_inspect_yield	=document.assemblyDataEntry.diceInspectYield.value;
	//var ic_finished_in		=document.assemblyDataEntry.icFinishedIn.value;
	//var ic_finished_out		=document.assemblyDataEntry.icFinishedOut.value;
	//var ic_finished_yield	=document.assemblyDataEntry.icFinishedYield.value;
	var inv_yield			=document.assemblyDataEntry.invYield.value;
//	var act_yield			=document.assemblyDataEntry.actYield.value;
	var asy_yield			=document.assemblyDataEntry.asyYield.value;

	var pa =document.getElementById("message");
	bool = true;

	/*if ( parseInt ( dice_inspect_in.trim().length ) > 0 &&  dice_inspect_in.trim() !='' )
	{
		if ( checkInteger( dice_inspect_in , 'Dice Inspect In ' , 'Y'  ) == false )
		{
			pa.innerHTML = "<li> "+errMsg;
			document.assemblyDataEntry.diceInspectIn.focus();
			bool = false;
			return bool;
		}
	}


	
	if ( dice_inspect_out.trim() != '' && dice_inspect_in.trim() != '' )
	{
		if ( checkInteger( dice_inspect_out , 'Dice Inspect Out ' , 'Y'  ) == false )
		{
			pa.innerHTML = "<li> "+errMsg;
			document.assemblyDataEntry.diceInspectOut.focus();
			bool = false;
			return bool;
		}

		if ( parseInt ( dice_inspect_out.trim() ) > parseInt ( dice_inspect_in.trim() ) )
		{
			pa.innerHTML = "<li> Dice Inspect Out cannot be greater than Dice Inspect In";
			document.assemblyDataEntry.diceInspectOut.focus();
			bool = false;
			return bool;
		}
	}
    if ( dice_inspect_out.trim() != '' && dice_inspect_in.trim() == '' )
	{
	    pa.innerHTML = "<li> Dice Inspect  In cannot be empty";
		document.assemblyDataEntry.diceInspectIn.focus();
		bool = false;
		return bool;
	}
	if ( dice_inspect_out.trim() == '' && dice_inspect_in.trim() == '' )
	{
	    document.assemblyDataEntry.diceInspectYield.value = "";
	}

	if(dice_inspect_yield!='')
	{
		if(!isFloat(dice_inspect_yield))
		{
			pa.innerHTML = "<li> Please enter valid Dice Inspect Yield,It must be float";
			document.assemblyDataEntry.diceInspectYield.focus();
			bool = false;
			return bool;
		}
	}
	if ( parseInt ( ic_finished_in.trim().length ) > 0 &&  ic_finished_in.trim() !='' )
	{
		if ( checkInteger( ic_finished_in , 'Ic Finished In ' , 'Y'  ) == false )
		{
			pa.innerHTML = "<li> "+errMsg;
			document.assemblyDataEntry.icFinishedIn.focus();
			bool = false;
			return bool;
		}
	}
	if ( ic_finished_out.trim() != '' && ic_finished_in.trim() != '' )
	{
		if ( checkInteger( ic_finished_out , 'Ic Finished Out ' , 'Y'  ) == false )
		{
			pa.innerHTML = "<li> "+errMsg;
			document.assemblyDataEntry.icFinishedOut.focus();
			bool = false;
			return bool;
		}

		if ( parseInt ( ic_finished_out.trim() ) > parseInt ( ic_finished_in.trim() ) )
		{
			pa.innerHTML = "<li> Ic Finished Out cannot be greater than Ic Finished In";
			document.assemblyDataEntry.icFinishedOut.focus();
			bool = false;
			return bool;
		}
	}

	if ( ic_finished_out.trim() != '' && ic_finished_in.trim() == '' )
	{
	    pa.innerHTML = "<li> Ic Finished In cannot be empty";
		document.assemblyDataEntry.icFinishedIn.focus();
		bool = false;
		return bool;
	}
	if ( ic_finished_out.trim() == '' && ic_finished_in.trim() == '' )
	{
	    document.assemblyDataEntry.icFinishedYield.value = "";
	}

	if( ic_finished_yield!='' )
	{
		if ( !isFloat ( ic_finished_yield.trim() ) )
		{
			pa.innerHTML = "<li> Please enter valid IC Finished Yield,It must be float";
			document.assemblyDataEntry.icFinishedYield.focus();
			bool = false;
			return bool;
		}
	}  */

	if(inv_yield!='')
	{
		if( !isFloat ( inv_yield.trim() ) )
		{
			pa.innerHTML = "<li> Please enter valid INV Yield,It must be float";
			document.assemblyDataEntry.invYield.focus();
			bool = false;
			return bool;
		}
	}

/*	if(act_yield!=''){
		if(!isFloat(act_yield)){
		pa.innerHTML = "Please enter valid ACT Yield,It must be float";
		document.assemblyDataEntry.actYield.select();
		document.assemblyDataEntry.actYield.focus();
		return;
		}
	}
*/

	if(asy_yield!='')
	{
		if(!isFloat(asy_yield))
		{
			pa.innerHTML = "<li> Please enter valid ASY Yield,It must be float";
			document.assemblyDataEntry.asyYield.focus();
			bool = false;
			return bool;
		}
	}
	return bool;
}

function trimAllBeforeSubmit()
{
	document.assemblyDataEntry.dateIn.value = document.assemblyDataEntry.dateIn.value.trim();
	document.assemblyDataEntry.dateOut.value = document.assemblyDataEntry.dateOut.value.trim();
	document.assemblyDataEntry.quantityIn.value = document.assemblyDataEntry.quantityIn.value.trim();
	document.assemblyDataEntry.quantityOut.value = document.assemblyDataEntry.quantityOut.value.trim();
	//document.assemblyDataEntry.diceInspectIn.value = document.assemblyDataEntry.diceInspectIn.value.trim();
	//document.assemblyDataEntry.diceInspectOut.value = document.assemblyDataEntry.diceInspectOut.value.trim();
	//document.assemblyDataEntry.diceInspectYield.value =document.assemblyDataEntry.diceInspectYield.value.trim();
	//document.assemblyDataEntry.icFinishedIn.value = document.assemblyDataEntry.icFinishedIn.value.trim();
	//document.assemblyDataEntry.icFinishedOut.value = document.assemblyDataEntry.icFinishedOut.value.trim();
	//document.assemblyDataEntry.icFinishedYield.value = document.assemblyDataEntry.icFinishedYield.value.trim();
	document.assemblyDataEntry.invYield.value = document.assemblyDataEntry.invYield.value.trim();
	document.assemblyDataEntry.asyYield.value = document.assemblyDataEntry.asyYield.value.trim();
	document.assemblyDataEntry.lotNo.value = document.assemblyDataEntry.lotNo.value.trim();
	document.assemblyDataEntry.lc.value = document.assemblyDataEntry.lc.value.trim();
}